<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

$_LangPath = "bethistorylist";
include_once(__Language_Path."/selectlanguage_member.php");

$arrToday = split("-", date("Y-m-d"));
$strSDate = date("Y-m-d", mktime(0, 0, 0, $arrToday[1], $arrToday[2] - 7, $arrToday[0]));

$PayupDate = $_data["PayupDate"];

if($strSDate > $PayupDate)$PayupDate = $strSDate;

$a = split("-", $PayupDate);
if(!checkdate($a[1], $a[2], $a[0])){
	$GLOBALS["errormessage"] = $_strBetHistoryList["ErrDate"];
	include_once(__Root_Path."/error.php");
	exit();
}

$Bdate = date("Y-m-d", mktime(0, 0, 0, $a[1], $a[2], $a[0]));
$Bweek = date("w", mktime(0, 0, 0, $a[1], $a[2], $a[0]));
include_once(__Member_Path."/logincheck_member_report.php");
include_once(__Modules_Path."/ChangeTime.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Modules_Path."/Lotto.lib.php");

//取出帳務
$strSQL = "select a.ID, a.LottoID, b.BetDate, b.BetTime, a.Number as Number, a.Bet_Type, b.Content, a.MemStake, a.MemResult, a.SubIncome, a.LottoID, a.isPayOff, b.OneStake from `bet_BetList` a , `bet_BetListContent` b where a.PayupDate = '".$PayupDate."' and a.MemID = ".$_SESSION["Member_Info"] -> ID." and a.ID = b.ID order by a.LottoID, a.ID desc";

if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Error";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$arrRows = array();
if(mysql_num_rows($RS) > 0){
	while($Rows = mysql_fetch_object($RS)){
		$arrRows[$Rows -> LottoID][] = $Rows;
	}
}
@mysql_free_result($RS);

//取的樣版
$fp = @file(__Membertemplate_Path."/bethistory_list.html");
$fp = implode("", $fp);
$fp = setHTMLCharset($fp);
$fp = str_replace("{Name}", $_SESSION["Member_Info"] -> Name, $fp);
$fp = str_replace("{ShowDate}", date("Y-m-d")." ".$_arrWeek[$Bweek], $fp);

//以下位置不可上下移動
while(list($k, $v) = each($_strBetHistoryList)){
	$fp = str_replace("{".$k."}", $v, $fp);
}

preg_match("/<!--@ LottoContent Start @\/\/-->(.+)<!--@ LottoContent End @\/\/-->/s", $fp, $d);
$LottoContent = $d[1];
preg_match("/<!--@ BetContent While Start @\/\/-->(.+)<!--@ BetContent While End @\/\/-->/s", $LottoContent, $d);
$BetContent = $d[1];

$League = 0;
$intLID = 0;

$All = 0;  //總筆數
$AllStake = 0;  //總下注金額
$AllResult = 0;  //總中獎金額
$AllComm = 0;  //總退水金額
$AllSubIncome = 0;  //退水後，總最高可得派彩

$arrLottoPower = getLottoUsePowerArray();
$arrOneStake[1] = array(7, 8, 9, 10, 11, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43);
$arrOneStake[2] = array();
$arrOneStake[3] = array();
$arrOneStake[4] = array(15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35);
foreach($_arrLotto as $intLID => $arrL){
	if(!in_array($intLID, $arrLottoPower))continue;

	$total = 0;
	$totalStake = 0;
	$totalResult = 0;
	$totalComm = 0;
	$totalSubIncome= 0;  //總最高可得派彩
	$strWCon = "";
	if(sizeof($arrRows[$intLID]) > 0){
		foreach($arrRows[$intLID] as $Rows){
			$tmpBetContent = str_replace("{BetID}", $Rows -> ID, $BetContent);
			$strTmpCon =  WagerFormTrans($Rows -> LottoID, $Rows -> Bet_Type, $Rows -> Content);
			
			$strTmp = "";
			if($Rows -> isPayOff >= 0){
				$total++;
				$totalStake += $Rows -> MemStake;
				$totalSubIncome += $Rows -> SubIncome;
				$totalResult += $Rows -> MemResult;
				$totalComm += ($Rows -> SubIncome - $Rows -> MemResult);
				$tmpBetContent = str_replace("{MemResult}", sprintf("%.1f", $Rows -> MemResult), $tmpBetContent);
				$tmpBetContent = str_replace("{MemComm}", sprintf("%.1f", $Rows -> SubIncome - $Rows -> MemResult), $tmpBetContent);
				$tmpBetContent = str_replace("{SubIncome}", sprintf("%.1f", $Rows -> SubIncome), $tmpBetContent);
			}else{
				$tmpBetContent = str_replace("{MemResult}", sprintf("%.1f", 0), $tmpBetContent);
				$tmpBetContent = str_replace("{MemComm}", sprintf("%.1f", 0), $tmpBetContent);
				$tmpBetContent = str_replace("{SubIncome}", $_strBetHistoryList["Cancel"], $tmpBetContent);
			} //if($Rows -> isPayOff >= 0)

			//if($arrL["LType"] == 1 && $Rows -> Bet_Type >= 7 && $Rows -> Bet_Type <= 11 && !empty($Rows -> OneStake)){
			if(in_array($Rows -> Bet_Type, $arrOneStake[$arrL["LType"]]) && !empty($Rows -> OneStake)){
				$WagerNo = round($Rows -> MemStake / $Rows -> OneStake);
				if($WagerNo > 1){
					$strTmp = '<span class="WagerCon_WagerNo_color">'.$Rows -> OneStake.' x '.$WagerNo.'</span><br /><br />';
				}
			}

			$tmpBetContent = str_replace("{MemStake}", $strTmp.$Rows -> MemStake, $tmpBetContent);
			$tmpBetContent = str_replace("{Content}", $strTmpCon, $tmpBetContent);
			$tmpBetContent = str_replace("{Lotto}", $_arrLotto[$Rows -> LottoID]["Lotto_".$_intLang], $tmpBetContent);
			$GD = DateForm($Rows -> BetDate, $Rows -> BetTime);
			$GT = TimeForm($Rows -> BetDate, $Rows -> BetTime, "H:i:s");
			$tmpBetContent = str_replace("{BetTime}", $GD." ".$GT, $tmpBetContent);
			if($arrL["LType"] == 1 && $Rows -> Bet_Type == 15){
				$tmpBetContent = str_replace("{MemBetType}", $_arrBet_Type[$arrL["LType"]][$Rows -> Bet_Type]."<br />".$Rows -> Number.$_strSame_String[Connection]."1", $tmpBetContent);
			}else{
				$tmpBetContent = str_replace("{MemBetType}", $_arrBet_Type[$arrL["LType"]][$Rows -> Bet_Type], $tmpBetContent);
			}
			$strWCon .= $tmpBetContent;
		} //foreach($arrRows[$intBall] as $Rows)
	} //if(sizeof($arrRows[$intLID]) > 0)
	$All += $total;
	$AllStake += $totalStake;
	$AllResult += $totalResult;
	$AllComm += $totalComm;
	$AllSubIncome += $totalSubIncome;

	$tmpLottoContent = str_replace("{LottoName}", $arrL["Lotto_".$_intLang], $LottoContent);
	$tmpLottoContent = preg_replace("/<!--@ BetContent While Start @\/\/-->.+<!--@ BetContent While End @\/\/-->/s", $strWCon, $tmpLottoContent);
	$tmpLottoContent = str_replace("{TotalNo}", $total, $tmpLottoContent);
	$tmpLottoContent = str_replace("{TotalStake}", $totalStake, $tmpLottoContent);
	$tmpLottoContent = str_replace("{TotalResult}", sprintf("%.1f", $totalResult), $tmpLottoContent);
	$tmpLottoContent = str_replace("{TotalComm}", sprintf("%.1f", $totalComm), $tmpLottoContent);
	$tmpLottoContent = str_replace("{TotalSubIncome}", sprintf("%.1f", $totalSubIncome), $tmpLottoContent);
	$strLCon .= $tmpLottoContent;
} //foreach($_arrLotto as $intLID => $arrL)

$fp = preg_replace("/<!--@ LottoContent Start @\/\/-->.+<!--@ LottoContent End @\/\/-->/s", $strLCon, $fp);
$fp = str_replace("{AllTotalNo}", $All, $fp);
$fp = str_replace("{AllTotalStake}", $AllStake, $fp);
$fp = str_replace("{AllTotalResult}", sprintf("%.1f", $AllResult), $fp);
$fp = str_replace("{AllTotalComm}", sprintf("%.1f", $AllComm), $fp);
$fp = str_replace("{AllTotalSubIncome}", sprintf("%.1f", $AllSubIncome), $fp);

//輸出
WebOutputHTML($fp, 1);
?>